using System;
using System.Net;
using System.IO;
using SecretLabs.NETMF.IO;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

public delegate void BytesDelegate(byte[] data); 


public class SDCard
{
    const int fileBufferSize = 256;

    public static Object SDCardLock = new Object();
    public const string DirectoryPath = "\\SD";
    protected string _FileName = "";
    //protected FileStream _FileStream = null;
    //protected StreamWriter _StreamWriter = null;

    public SDCard(string fileName)
    {
        _FileName = fileName;
    }

    ~SDCard()
    {
    }

    public string FileFullPath
    {
        get 
        {
            return DirectoryPath + "\\" + _FileName;
        }
    }

    public void WriteLine(string text)
    {
        lock (SDCardLock)
        {
            FileStream fs = new FileStream(FileFullPath, FileMode.Append, FileAccess.Write, FileShare.None, 512);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(text);
            sw.Flush();
            sw.Close();
            fs.Close();
        }
    }

    public void WriteLines(string[] lines)
    {

        lock (SDCardLock)
        {
            FileStream fs = new FileStream(FileFullPath, FileMode.Append, FileAccess.Write, FileShare.None, 512);
            StreamWriter sw = new StreamWriter(fs);
            for (int i = 0; i < lines.Length - 1; i++)
            {
                sw.WriteLine(lines[i]);
            }
            sw.Flush();
            sw.Close();
            fs.Close();
        }      
   }


    public bool Read(string fullFilePath, int startChar, int charCount)
    {
        lock (SDCardLock)
        {
            Debug.Print("filename: " + fullFilePath);
            FileStream fs = new FileStream(fullFilePath, FileMode.Open, FileAccess.Read, FileShare.None, 512);
            StreamReader sr = new StreamReader(fs);
 ////           sr.BaseStream.
 //           char[] buffer = char[512];
 //           contents = sr.Read(
 //           Debug.Print("contents: " + sr.ReadToEnd());
 //           return contents;
            return true; //TODO
        }
    }



    public void ReadInChunks(string fullFilePath, BytesDelegate sendDataCallback)
    {
        lock (SDCardLock)
        {
            Debug.Print("Reading file: " + fullFilePath);
            using (FileStream inputStream = new FileStream(fullFilePath, FileMode.Open))
            {
                byte[] readBuffer = new byte[fileBufferSize];
                while (true)
                {
                    // Send the file a few bytes at a time
                    int bytesRead = inputStream.Read(readBuffer, 0, readBuffer.Length);
                    if (bytesRead == 0)
                        break;

                    sendDataCallback(readBuffer);
                    Debug.Print("Sending " + readBuffer.Length.ToString() + "bytes...");
                }
            }

        }
    }

    public long GetFileSize(string fullFilePath)
    {
        long result = 0;
        lock (SDCardLock)
        {
            Debug.Print("Get File Size: " + fullFilePath);
            using (FileStream inputStream = new FileStream(fullFilePath, FileMode.Open))
            {
                result = inputStream.Length;
            }
        }
        return result;
    }

    //public static void ReadFromBufferedFileStreamAndOutputTimingToConsole(string sourceFile)
    //{

    //    if (!File.Exists(sourceFile))
    //    {

    //        throw new ArgumentException("You must supply a valid file to read from.");

    //    }
    //    long fileSize = new FileInfo(sourceFile).Length;
    //    using (FileStream sourceFileStream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read))
    //    {
    //        using (BufferedStream bufferedStream = new BufferedStream(sourceFileStream, 5242880))
    //        {
    //            using (StreamReader reader = new StreamReader(bufferedStream))
    //            {
    //                while (reader.Peek() >= 0)
    //                {
    //                    reader.ReadLine();
    //                }
    //            }
    //        }
    //    }

    //}

    //public string Read()
    //{
    //    lock (SDCardLock)
    //    {
    //        string[] directories = System.IO.Directory.GetDirectories(@"\");
    //        Debug.Print("directory count: " + directories.Length.ToString());

    //        for (int i = 0; i < directories.Length; i++)
    //        {
    //            Debug.Print("directory: " + directories[i]);
    //        }

    //        string[] files = System.IO.Directory.GetFiles(@"\SD");
    //        Debug.Print("file count: " + files.Length.ToString());

    //        string contents = "";
    //        for (int i = 0; i < files.Length; i++)
    //            {
    //            Debug.Print("filename: " + files[i]);
    //            FileStream fs = new FileStream(files[i], FileMode.Open, FileAccess.Read, FileShare.None, 512);
    //            StreamReader sr = new StreamReader(fs);
    //            contents = sr.ReadToEnd()
    //            Debug.Print("contents: " + sr.ReadToEnd());
    //        }
    //        return contents;
    //    }
    //}

}



